package io.renren.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.shop.entity.*;
import io.renren.modules.shop.service.GoodsAndOrderService;
import io.renren.modules.shop.service.GoodsMsgService;
import io.renren.modules.shop.service.GoodsOrderService;
import io.renren.modules.shop.service.WxUserService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("goods_and_order")
@Api(value = "货物和订单详情controller", tags = {"货物和订单详情接口"})
@Log4j2
public class GoodsAndOrderController extends AbstractController {
    // 详情
    @Autowired
    private GoodsAndOrderService goodsAndOrderService;

    // 总表
    @Autowired
    private GoodsOrderService goodsOrderService;

    @Autowired
    private GoodsMsgService goodsMsgService;

    @ApiOperation(value = "查询分页")
    @PostMapping("page")
    public R getPage(@ApiParam("订单对象信息")@RequestBody GoodsAndOrder goodsAndOrder, @ApiParam("每页个数")@RequestParam Integer size, @ApiParam("页数")@RequestParam Integer page) {
        IPage<GoodsAndOrder> iPage = new Page<>(page,size);
        IPage<GoodsAndOrder> goodsCouponIPage = goodsAndOrderService.page(iPage, new QueryWrapper<>(goodsAndOrder));
        PageUtils pageResult = new PageUtils(goodsCouponIPage);
        return R.ok().put("page", pageResult);
    }

    /**
     *
     * @param goodsMsg 商品
     * @param num 新增的数量
     * @return
     */
    @ApiOperation(value = "添加商品到购物车")
    @PostMapping("add")
    public R addNew(@ApiParam("商品信息")@RequestBody GoodsMsg goodsMsg,@ApiParam("商品数量") @RequestParam Integer num) {
        boolean flag = goodsAndOrderService.addCar(goodsMsg, getOpenId(), num);
        return R.ok().put("flag", flag);
    }


    @ApiOperation(value = "查询所有数据")
    @PostMapping("list")
    public R getList(@RequestBody GoodsAndOrder goodsAndOrder) {
        List<GoodsAndOrder> list = goodsAndOrderService.list(new QueryWrapper<>(goodsAndOrder));
        return R.ok().put("list", list);
    }

    @ApiOperation(value = "删除购物车商品")
    @PostMapping("delete")
    public R del(@ApiParam("商品信息")@RequestBody GoodsMsg goodsMsg,@ApiParam("商品数量") @RequestParam Integer num) {
        boolean flag = goodsAndOrderService.delCar(goodsMsg, getOpenId(), num);
        return R.ok().put("flag", flag);
    }

    @ApiOperation(value = "直接购买商品")
    @PostMapping("buyOne")
    public R buyOne(@ApiParam("商品信息")@RequestParam Integer id,@ApiParam("商品数量") @RequestParam Integer num,@ApiParam("优惠券ID，无优惠券请发送0") @RequestParam Integer goodsCouponId) {
        log.info("这是传递的数据" + id);
        log.info("这是传递的数据" +num);
        log.info("这是传递的数据" + goodsCouponId);
        goodsAndOrderService.buyOne(goodsMsgService.getById(id),getOpenId(),num,goodsCouponId);
        return R.ok();
    }

    @ApiOperation(value = "订单取消")
    @PostMapping("cancelOrder")
    public R cancelOrder(@ApiParam("总订单表的id")@RequestParam Integer goodsOrderId) {
        boolean flag = goodsAndOrderService.cancelOrder(goodsOrderId);
        return R.ok().put("flag", flag);
    }

}
